﻿using System;
using System.Web;
using System.Web.Mvc;

namespace doufu.Common
{
    public class CacheFilter : ActionFilterAttribute
    {
        /// <summary>
        /// 分钟 默认为：10 Minutes
        /// </summary>
        /// <value>The cache duration in Minutes.</value>
        public int Duration
        {
            get;
            set;
        }

        public CacheFilter()
        {
            Duration = 10;
        }
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (Duration <= 0) return;
            filterContext.HttpContext.Response.ClearHeaders();
            HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
            TimeSpan cacheDuration = TimeSpan.FromMinutes(Duration);
            cache.SetCacheability(HttpCacheability.Public);
            cache.SetExpires(DateTime.Now.Add(cacheDuration));
            cache.SetMaxAge(cacheDuration);
            cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
            base.OnActionExecuted(filterContext);
        }
    }
}